package io.renren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import dm.jdbc.util.StringUtil;
import io.renren.common.constant.Constant;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.DateUtils;
import io.renren.dao.*;
import io.renren.dto.RepairquotationmDTO;
import io.renren.dto.ReservationDto;
import io.renren.dto.repon.RepairquotationmRepon;
import io.renren.entity.*;
import io.renren.service.RepairquotationmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * ${comments}
 *
 * @author Sen 524227263@qq.com
 * @since 1.0.0 2024-05-28
 */
@Service
public class RepairquotationmServiceImpl extends CrudServiceImpl<RepairquotationmDao, RepairquotationmEntity, RepairquotationmDTO> implements RepairquotationmService {

    @Autowired
    private RepairquotationitemsdDao repairquotationitemsdDao;
    @Autowired
    private RepairquotationpickingdDao repairquotationpickingdDao;
    @Autowired
    private AutoDao autoDao;
    @Autowired
    private ChatgroupDao chatgroupDao;
    @Autowired
    private ReservationmDao reservationmDao;


    @Override
    public QueryWrapper<RepairquotationmEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<RepairquotationmEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public List<RepairquotationmRepon> selectListByIsCustomerConfirm(String traderMOID, String type) {
        QueryWrapper<RepairquotationmEntity> wrapper = new QueryWrapper<>();
        wrapper.select("a.SYS_Created,a.RepairQuotationMOID,a.Amount,a.UCML_CONTACT_FK,a.Auto_FK,b.OrgName,c.PersonName,d.AutoNo");
        wrapper.eq("a.TraderM_FK", traderMOID);
        if (StringUtil.isNotEmpty(type)) {
            wrapper.eq("a.IsCustomerConfirm", type);
            if ("0".equals(type)) {
                wrapper.eq("a.QuotationState", 4);
            }
            if ("1".equals(type)) {
                wrapper.eq("a.QuotationState", 5);
            }
        }
        List<RepairquotationmRepon> repairquotationmRepons = baseDao.selectListByIsCustomerConfirm(wrapper);

        int indexNO = 1;
        for (RepairquotationmRepon item : repairquotationmRepons) {
            // 服务项目
            QueryWrapper<RepairquotationitemsdEntity> wrapperRepairquotation = new QueryWrapper<>();
            wrapperRepairquotation.select("ServiceName,CustomConfirmSign");
            wrapperRepairquotation.eq("RepairQuotationM_FK", item.getRepairQuotationMOID());
            List<RepairquotationitemsdEntity> repairquotationitemsdEntities = repairquotationitemsdDao.selectList(wrapperRepairquotation);

            List<String> serviceInfo = new ArrayList<>();
            for (RepairquotationitemsdEntity repair : repairquotationitemsdEntities) {
                if ("0".equals(repair.getCustomconfirmsign())) {
                    continue;
                }
                serviceInfo.add(repair.getServicename());
            }
            if (serviceInfo.size() > 0) {
                item.setServiceNames(serviceInfo.stream().collect(Collectors.joining(",")));
            }
            // 材料
            QueryWrapper<RepairquotationpickingdEntity> wrapperPickingd = new QueryWrapper<>();
            wrapperPickingd.select("GoodsName,CustomConfirmSign");
            wrapperPickingd.eq("RepairQuotationM_FK", item.getRepairQuotationMOID());
            List<RepairquotationpickingdEntity> pickingdList = repairquotationpickingdDao.selectList(wrapperPickingd);

            List<String> pickingdArr = new ArrayList<>();
            for (RepairquotationpickingdEntity info : pickingdList) {
                if ("0".equals(info.getCustomconfirmsign())) {
                    continue;
                }
                pickingdArr.add(info.getGoodsname());
            }
            if (serviceInfo.size() > 0) {
                item.setMaterialNames(pickingdArr.stream().collect(Collectors.joining(",")));
                item.setSysCreated(DateUtils.parseToStr(item.getSysCreated(), "yyyy-MM-dd HH:mm:ss"));
            }

            // 查询群聊ID
            QueryWrapper<AutoEntity> wrapperAuro = new QueryWrapper<>();
            wrapperAuro.eq("AutoOID", item.getAutoFk());
            List<AutoEntity> autoEntities = autoDao.selectList(wrapperAuro);
            if (autoEntities.size() > 0) {
                AutoEntity autoEntity = autoEntities.get(0);
                QueryWrapper<ChatgroupEntity> wrapperChatGroup = new QueryWrapper<>();
                wrapperChatGroup.eq("IMGroupID", autoEntity.getAutoframenum());
                List<ChatgroupEntity> chatgroupEntities = chatgroupDao.selectList(wrapperChatGroup);
                if (chatgroupEntities.size() > 0) {
                    ChatgroupEntity cgEntity = chatgroupEntities.get(0);
                    item.setGroupId(cgEntity.getImgroupid());
                }
            }

            DecimalFormat df = new DecimalFormat("#.00");
            item.setAmount(df.format(Double.valueOf(item.getAmount())));

            item.setRowNumber(indexNO + "");
            indexNO++;
        }
        return repairquotationmRepons;
    }

    @Override
    public RepairquotationmEntity findOneByRepairQuotationMOID(String repairQuotationMOID) {
        QueryWrapper<RepairquotationmEntity> wrapper = new QueryWrapper<>();
        wrapper.select("Org_FK,UCML_CONTACT_FK,Auto_FK,TotalAmount,BillDate,RepairQuotationMOID,IdentityDiscountAmount,CardDeductionAmount,Amount,IsCustomerConfirm");
        wrapper.eq("RepairQuotationMOID", repairQuotationMOID);
        List<RepairquotationmEntity> repairquotationmEntities = baseDao.selectList(wrapper);
        return repairquotationmEntities.size() > 0 ? repairquotationmEntities.get(0) : null;
    }

    @Override
    public void quotationConfirm(String traderMOID, String repairQuotationMOID) {

        RepairquotationmEntity info = new RepairquotationmEntity();
        info.setQuotationstate("5");
        info.setIscustomerconfirm(true); // true 1 ，false 0
        info.setCustomerconfirmtime(new Date());

        baseDao.update(info, new QueryWrapper<RepairquotationmEntity>().eq("TraderM_FK", traderMOID).eq("RepairQuotationMOID", repairQuotationMOID));

    }


    @Override
    public void reservation(String traderMOID, ReservationDto dto) {
    }
}